package com.zhida.smartcontrol.pop;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jay.tools.base.BasePopWindow;
import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.DimenUtils;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;

/**
 * author: tanjianpei
 * date: 2019/10/10 010 10:42
 * description: 单灯
 */
public class DandengPop extends BasePopWindow implements View.OnClickListener {
    private TextView mTvClassify;
    private TextView mTvName;
    private TextView mTvTime;
    private TextView mTvGroup;
    private TextView mTvNameType;
    private ButtonStyle mBtnOnline;
    private TextView mTvAddress;
    private ImageView mIVLight;
    private ImageView mIVXinhao;
    private ButtonStyle mBtnOpen, mBtnClose, mBtnDimmer, mBtnJkq, mBtnNb;
    private ButtonStyle mBtnAuto;
    private ButtonStyle mBtnHand;

    private LampBean mBean;

    public DandengPop(Context context) {
        super(context);
        setWidth(DimenUtils.dp2px(mContext, 345));
    }

    public void bindData(LampBean bean) {
        mBean = bean;
        mTvClassify.setText(bean.getSwitchTypeTxt());

        mTvName.setText("灯杆ID：" + bean.getImei());

        mTvTime.setText("更新时间：" + bean.getUpdateDate());
        mTvGroup.setText(bean.getGroupName());

        if (bean.getOnlineStatus().equals("0")) {
            mBtnOnline.setText("离线");
            mBtnOnline.setChecked(false);
        } else if (bean.getOnlineStatus().equals("1")) {
            mBtnOnline.setText("在线");
            mBtnOnline.setChecked(true);
        }
        mTvAddress.setText(bean.getShortAddress());
        mTvNameType.setText(bean.getAddress());
        if (bean.getSignalStrength().equals("0")) {
            mIVXinhao.setImageResource(R.drawable.icon_single_0);
        } else if (bean.getSignalStrength().equals("1") || bean.getSignalStrength().equals("2") || bean.getSignalStrength().equals("3") || bean.getSignalStrength().equals("4") || bean.getSignalStrength().equals("5")) {
            mIVXinhao.setImageResource(R.drawable.icon_single_1);
        } else if (bean.getSignalStrength().equals("6") || bean.getSignalStrength().equals("7") || bean.getSignalStrength().equals("8") || bean.getSignalStrength().equals("9") || bean.getSignalStrength().equals("10")) {
            mIVXinhao.setImageResource(R.drawable.icon_single_2);
        } else if (bean.getSignalStrength().equals("11") || bean.getSignalStrength().equals("12") || bean.getSignalStrength().equals("13") || bean.getSignalStrength().equals("14") || bean.getSignalStrength().equals("15")) {
            mIVXinhao.setImageResource(R.drawable.icon_single_3);
        } else if (bean.getSignalStrength().equals("16") || bean.getSignalStrength().equals("17") || bean.getSignalStrength().equals("18") || bean.getSignalStrength().equals("19") || bean.getSignalStrength().equals("20")) {
            mIVXinhao.setImageResource(R.drawable.icon_single_3);
        } else {
            mIVXinhao.setImageResource(R.drawable.icon_single_5);
        }
        if (bean.getSwitchType().equals(Const.NB_SINGLE)) {
            if (bean.getIsOpen().equals("0")) {
                mIVLight.setImageResource(R.drawable.icon_light_n);
            } else {
                mIVLight.setImageResource(R.drawable.icon_light_s);
            }
        } else if (bean.getSwitchType().equals(Const.NB_DOUBLE)) {
            if (bean.getIsOpen().equals("00")) {
                mIVLight.setImageResource(R.drawable.icon_light_n);
            } else {
                mIVLight.setImageResource(R.drawable.icon_light_s);
            }
        }
        String controlType = bean.getControlType();
        if (controlType.equals("0")) { //自动模式
            mBtnOpen.setVisibility(View.GONE);
            mBtnClose.setVisibility(View.GONE);
            mBtnDimmer.setVisibility(View.GONE);
            mBtnJkq.setVisibility(View.GONE);
            mBtnNb.setVisibility(View.VISIBLE);
            mBtnAuto.setEnabled(false);
            mBtnHand.setEnabled(true);

//            setHeight(DimenUtils.dp2px(mContext, 165));
        } else { // 手动
            mBtnHand.setEnabled(false);
            mBtnAuto.setEnabled(true);
            mBtnOpen.setVisibility(View.VISIBLE);
            mBtnClose.setVisibility(View.VISIBLE);
            mBtnDimmer.setVisibility(View.VISIBLE);
            mBtnJkq.setVisibility(View.VISIBLE);
            mBtnNb.setVisibility(View.GONE);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_home_dd;
    }

    @Override
    protected void initViews(View v) {
        mTvClassify = v.findViewById(R.id.tv_classify);
        mTvName = v.findViewById(R.id.tv_name);
        mTvTime = v.findViewById(R.id.tv_time);
        mTvGroup = v.findViewById(R.id.tv_group_name);
        mTvNameType = v.findViewById(R.id.tv_name_type);
        mBtnOnline = v.findViewById(R.id.btn_online);
        mTvAddress = v.findViewById(R.id.tv_address);
        mBtnOpen = v.findViewById(R.id.btn_open);
        mBtnClose = v.findViewById(R.id.btn_close);
        mBtnDimmer = v.findViewById(R.id.btn_dimmer);
        mBtnAuto = v.findViewById(R.id.btn_auto);
        mBtnHand = v.findViewById(R.id.btn_hand);
        mBtnJkq = v.findViewById(R.id.btn_jkq);
        mIVLight = v.findViewById(R.id.iv_light);
        mIVXinhao = v.findViewById(R.id.iv_xinhao);
        mBtnNb = v.findViewById(R.id.btn_nb);
    }

    @Override
    protected void initData() {
// 1、2两个参数表示左上角，3、4表示右上角，5、6表示右下角，7、8表示左下角
        float[] floatAuto = {20, 20, 0, 0, 0, 0, 20, 20};
        for (int i = 0; i < floatAuto.length; i++) {
            floatAuto[i] = DimenUtils.dp2px(mContext, floatAuto[i]);
        }
        mBtnAuto.setRadius(floatAuto);

        float[] floatHand = {0, 0, 20, 20, 20, 20, 0, 0};
        for (int i = 0; i < floatHand.length; i++) {
            floatHand[i] = DimenUtils.dp2px(mContext, floatHand[i]);
        }
        mBtnHand.setRadius(floatHand);
    }

    @Override
    protected void initEvents() {
        mBtnAuto.setOnClickListener(this);
        mBtnHand.setOnClickListener(this);
        mBtnOpen.setOnClickListener(this);
        mBtnClose.setOnClickListener(this);
        mBtnDimmer.setOnClickListener(this);
        mBtnJkq.setOnClickListener(this);
    }
    public void changeMode(int type) {
        // 0自动，1手动
        if (type == 0) {
            mBtnAuto.setEnabled(false);
            mBtnHand.setEnabled(true);
            mBtnOpen.setVisibility(View.GONE);
            mBtnClose.setVisibility(View.GONE);
            mBtnDimmer.setVisibility(View.GONE);
            mBtnJkq.setVisibility(View.GONE);
            mBtnNb.setVisibility(View.VISIBLE);
        } else {
            mBtnAuto.setEnabled(true);
            mBtnHand.setEnabled(false);
            mBtnOpen.setVisibility(View.VISIBLE);
            mBtnClose.setVisibility(View.VISIBLE);
            mBtnDimmer.setVisibility(View.VISIBLE);
            mBtnJkq.setVisibility(View.VISIBLE);
            mBtnNb.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int tag = -1;
        switch (v.getId()) {
            case R.id.btn_auto:
                tag=5;
                break;
            case R.id.btn_hand:
                tag=6;
                break;
            case R.id.btn_open:
                tag = 1;
                break;
            case R.id.btn_close:
                tag = 2;
                break;
            case R.id.btn_dimmer:
                tag = 3;
                break;
            case R.id.btn_jkq:
                tag = 4;
                break;
        }
        if (null != mListener) {
            mListener.onClick(mBean, tag);
        }
    }

    @Override
    protected int setCustomHeight() {
        return DimenUtils.dp2px(mContext, 180);
    }


}
